//
// Created by sukai on 22-9-19.
//

#include <iostream>
#include <ros/ros.h>
#include <signal.h>
#include "contnav_msgs/Dictionaries.h"
#include "contnav_msgs/DictionariesVector.h"
#include "contnav_srvs/ContnavLogger.h"
#include "common.h"
#include <queue>
using namespace std;
/**
 *
 * @param argc
 * @param argv
 * @return
 * 测试节点
 * 2022-09-19
 * sukai
 * rosrun contnav test_node
 */



#define contnav_debug(className,classLine,format, ...) \
    saveLogFormat(ContnavLoggerStr::DEBUG,className,classLine,format, ##__VA_ARGS__ )

#define contnav_info(className,classLine,format, ...) \
    saveLogFormat(ContnavLoggerStr::INFO,className,classLine,format, ##__VA_ARGS__ )
#define contnav_warn(className,classLine,format, ...) \
    saveLogFormat(ContnavLoggerStr::WARN,className,classLine,format, ##__VA_ARGS__ )
#define contnav_error(className,classLine,format, ...) \
    saveLogFormat(ContnavLoggerStr::ERROR,className,classLine,format, ##__VA_ARGS__ )
#define contnav_fatal(className,classLine,format, ...) \
    saveLogFormat(ContnavLoggerStr::FATAL,className,classLine,format, ##__VA_ARGS__ )

map<string, string> dictionariesMap_;
ros::ServiceClient   contnavLoggerClient;
string logFileName="testlog.txt";
struct node1{
    int data;
}a[10];

//ctrl + c
void mySigintHandler(int sig)
{
    std::cout << "ctrl c  sig: "<<sig<<std::endl;

    ros::shutdown();
    if (ros::isShuttingDown()){
        cout<<"close ros  test.cpp"<<endl;
    }
}
//日志级别 日志内容
bool saveLogFormat(string logLevel,string className,int classLine, char* format, ... ){

    string logcontent="";
    va_list arg_ptr;
    va_start(arg_ptr, format);
    int len = vsnprintf(NULL, 0, format, arg_ptr);
    va_end(arg_ptr);
    if (len > 0)
    {
        char * content = new char[len + 1];
        va_start(arg_ptr, format);
        vsnprintf(content, len + 1, format, arg_ptr);
        va_end(arg_ptr);
        content[len] = 0;
        logcontent = content;
        delete [] content; //动态的数组需要删除
       // m_len += len;//获取到当前日志的长度 累计起来
    }
    cout<<"logcontent: "<<logcontent<<endl;

    //查询字典数据
  //  contnavLoggerClient.waitForExistence();
    contnav_srvs::ContnavLogger contnavLoggerService;
    contnavLoggerService.request.request_type = ContnavLoggerType::savelog;// 保存日志   ContnavLoggerType::savelog:保存日志||   ContnavLoggerType::deleteAlllog:删除所有日志文件  ||  ContnavLoggerType::showlogSize 查看日志大小
    contnavLoggerService.request.logFileName = logFileName;// 日志文件名称
    contnavLoggerService.request.className = className;// 代码file名称 [注意： 确保每个服务节点只使用一个日志文件名,日志服务节点是根据文件名来创建对应的日志对象，请确保一个服务对应一个日志文件名称]
    contnavLoggerService.request.classLine = classLine;// 代码行数据
    contnavLoggerService.request.logLevel = logLevel;//日志类型,默认 debug
    contnavLoggerService.request.logcontent = logcontent;//日志内容

    //发送seervice
    contnavLoggerClient.call(contnavLoggerService);

    if(contnavLoggerService.response.result!="ok"){

        cout<<"close ros  test.cppcontnavLoggerService.response.message: "<<contnavLoggerService.response.message<<endl;
        return false;
    }
        return true;


}

//日志级别 16进制日志内容
bool saveHexLogFormat(string logLevel,string className,int classLine,vector<int> &logHexcontent ){

    //查询字典数据
   // contnavLoggerClient.waitForExistence();
    contnav_srvs::ContnavLogger contnavLoggerService;
    contnavLoggerService.request.request_type = ContnavLoggerType::saveHexlog;// 保存日志   ContnavLoggerType::savelog:保存日志|| saveHexlog:保存16进制报文日志  ContnavLoggerType::deleteAlllog:删除所有日志文件  ||  ContnavLoggerType::showlogSize 查看日志大小
    contnavLoggerService.request.logFileName = logFileName;// 日志文件名称
    contnavLoggerService.request.className = className;// 代码file名称 [注意： 确保每个服务节点只使用一个日志文件名,日志服务节点是根据文件名来创建对应的日志对象，请确保一个服务对应一个日志文件名称]
    contnavLoggerService.request.classLine = classLine;// 代码行数据
    contnavLoggerService.request.logLevel = logLevel;//日志类型,默认 debug
    for (int j = 0; j < logHexcontent.size(); ++j) {
        contnavLoggerService.request.logHexcontent.push_back(logHexcontent[j]);
    }
    //发送seervice
    contnavLoggerClient.call(contnavLoggerService);

    if(contnavLoggerService.response.result!="ok"){

        cout<<"close ros  test.cppcontnavLoggerService.response.message: "<<contnavLoggerService.response.message<<endl;
        return false;
    }
        return true;


}







// rosrun contnav testLog_node
int main(int argc,char *argv[]){
    //节点名
    string nodeName = "testLog_contnav_contnav_global_planner_node";
    //初始化节点
    ros::init(argc,argv,nodeName);
    //创建节点
    ros::NodeHandle node;
    ros::NodeHandle private_node_("~");
    //监控 ctrl c
    signal(SIGINT, mySigintHandler);

    private_node_.param<string>("logFileName", logFileName,logFileName);//日志名称
    //日志写入
    contnavLoggerClient = node.serviceClient<contnav_srvs::ContnavLogger::Request, contnav_srvs::ContnavLogger::Response>(
            "/contnavLogger");


    //1.===========日志写入===================================================================================

    //  bool fig= saveLogFormat(ContnavLoggerStr::ERROR,"nihao %f 你是谁 %d 你在干嘛 %s", 0.5,111,"钱" );
    contnav_debug(__FILE__,__LINE__,"1.nihao %f 你是谁 %d 你在干嘛 %s",111.5,111,"11钱");
    contnav_info(__FILE__,__LINE__," 2.nihao %f 你是谁 %d 你在干嘛 %s",111.5,111,"11钱");
    contnav_warn(__FILE__,__LINE__," 333333");
    contnav_error(__FILE__,__LINE__,"444444");
    contnav_fatal(__FILE__,__LINE__,"555555");

    string aa="获取直线 路径失败";
    contnav_error(__FILE__,__LINE__,const_cast<char*>(aa.c_str()));


   //2.日志写入=======================记录16进制日志内容===================================
    //日志级别 16进制日志内容
    vector<int> logHexcontent;
    logHexcontent.push_back((int)0xfe);
    logHexcontent.push_back((int)0x10);
    logHexcontent.push_back((int)0x15);
    logHexcontent.push_back((int)0x16);
    logHexcontent.push_back((int)0x17);

    saveHexLogFormat(ContnavLoggerStr::DEBUG,__FILE__,__LINE__,logHexcontent );
    saveHexLogFormat(ContnavLoggerStr::INFO,__FILE__,__LINE__,logHexcontent );
    saveHexLogFormat(ContnavLoggerStr::WARN,__FILE__,__LINE__,logHexcontent );
    saveHexLogFormat(ContnavLoggerStr::ERROR,__FILE__,__LINE__,logHexcontent );
    saveHexLogFormat(ContnavLoggerStr::FATAL,__FILE__,__LINE__,logHexcontent );


    //===========查看日志文件夹大小===================================================================================
    /**
    //查看日志文件夹大小
    contnav_srvs::ContnavLogger showcontnavLoggerService;
    showcontnavLoggerService.request.request_type = ContnavLoggerType::showlogSize;// 保存日志   ContnavLoggerType::savelog:保存日志||   ContnavLoggerType::deleteAlllog:删除所有日志文件  ||  ContnavLoggerType::showlogSize 查看日志大小
    //发送seervice
    contnavLoggerClient.call(showcontnavLoggerService);

    if(showcontnavLoggerService.response.result!=ServiceResultType::ResultTypeOK){
        cout<<"查看日志文件夹大小失败: "<<showcontnavLoggerService.response.message<<endl;
    }else{
        cout<<"查看日志文件夹大小成功: "<<showcontnavLoggerService.response.logSize<<endl;
    }
   */

    //===========删除所有日志文件===================================================================================
    /**
    //删除所有日志文件
    contnav_srvs::ContnavLogger deletecontnavLoggerService;
    deletecontnavLoggerService.request.request_type = ContnavLoggerType::deleteAlllog;// 保存日志   ContnavLoggerType::savelog:保存日志||   ContnavLoggerType::deleteAlllog:删除所有日志文件  ||  ContnavLoggerType::showlogSize 查看日志大小
    //发送seervice
    contnavLoggerClient.call(deletecontnavLoggerService);

    if(deletecontnavLoggerService.response.result!=ServiceResultType::ResultTypeOK){
        cout<<"close ros  test.cppcontnavLoggerService.response.message: "<<deletecontnavLoggerService.response.message<<endl;
    }else{
        cout<<"删除所有日志文件成功！"<<endl;
    }
    */
    //事件轮询
    ros::spin();
    return 0;
}


